Skip to content

feat(forge): notify router + snapshot apply (RIG-2732) - #676

Merged
mattwilkinsonn merged 6 commits into
mainfrom
compass-forge/rig-2732-t4-router
Aug 27, 2026
Merged

feat(forge): notify router + snapshot apply (RIG-2732)#676
mattwilkinsonn merged 6 commits into
mainfrom
compass-forge/rig-2732-t4-router

Conversation

@rigel-mintaka

@rigel-mintaka rigel-mintaka commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

This PR is part of a stack containing 4 PRs:

  1. main
  2. feat(forge): container-scope subscriptions, notification kinds, forge ack frame (RIG-2732) #674
  3. feat(forge): webhook ingress + event normalize (RIG-2732) #675
  4. "feat(forge): notify router + snapshot apply (RIG-2732)" (this PR)
  5. feat(forge): reconcile sweep + conditional reads (RIG-2732) #677

T4: the ForgeEvent hot path — NotifyRouter.Route + pure ApplyEvent/SnapshotRevision.

  • notify_router.go: package-local structural seams (NotifyStore, NotifyDispatcher, ChecksRoller) + NotifyRouter. Route runs the frozen algorithm (design.md:841-872): load prior snapshot -> resolve CHECKS roll-up before apply -> ApplyEvent -> upsert cursor BEFORE notify -> resolve subs (OPENED=container/project-matched, per-artifact=exact coordinate) -> dispatch a ForgeNotification carrying revision. Never advances delivered_revision (W3, ack-driven).
  • notify_detect.go: ArtifactSnapshot canonical form + pure ApplyEvent + SnapshotRevision (sha256 of canonical JSON, sorted checks, map-keyed comments) — the cross-producer meeting-point contract shared with T5.
  • Red-first unit tests with fakes for both seams, incl. the meeting-point invariant.

Reconciled seams (single-admissible readings, not open forks): NotifyStore gained LoadArtifactCursor (the frozen 3-method block omits the point read the algorithm's step 1 needs); ChecksRoller returns a placeholder ChecksResult in this commit, collapsed into the real forge.ConditionalResult[forge.Checks] by T5 stacked above; UPDATE/REVIEW are snapshot-neutral and labels are excluded from the digest (the ForgeEvent currency carries no title/body/labels, so the canonicalization invariant holds trivially).

Refs RIG-2732

Co-authored-by: Matt Wilkinson matt@rigel.build

… ack frame (RIG-2732)

Piece-2 T3: the wire + store slice for forge agent-notification.

## Proto (additive, one regen; buf-breaking-safe)

- `agent_gateway.proto`: new `ForgeSubscriptionScope` enum (UNSPECIFIED=0 / ARTIFACT=1 / CONTAINER=2) and two additive `SubscribeForgeRequest` fields `scope=4`, `project=5` (fields 1-3 untouched).
- `forge.proto`: two additive `ForgeNotificationKind` values `REVIEW=5`, `OPENED=6`.
- `agent.proto`: one additive `AgentFrame` oneof variant `forge_notification_ack=8` (next free field after `transcript_entry=7`) + the `ForgeNotificationAck { subscription_id, revision }` message, the forge sibling of `DeliveryAck`.
- Regenerated Go (`go/internal/gen/`) and the agent TS client via `moon run compass-proto:gen`.

## DDL (`0001_init.sql`, edited in place — greenfield single migration)

- `agent_forge_subscriptions` gains `scope SMALLINT NOT NULL DEFAULT 1 CHECK (scope IN (1,2))` and `project TEXT NOT NULL DEFAULT ''`.
- UNIQUE widened to (agent_account_id, forge_provider, forge_host, repo, kind, number, project) so two Linear project containers on one team don't collide.
- `forge_artifact_cursors` unchanged (admits number=0 container rows; needs no project column).

## Store (`forge_subscriptions.go`)

- `ForgeSubscriptionScope` type + `normalizeScope` (UNSPECIFIED -> ARTIFACT; store never persists 0).
- `validSubscriptionCoordinate` learns scope: ARTIFACT => number>0 AND project empty; CONTAINER => number==0, project REQUIRED on LINEAR / FORBIDDEN elsewhere. Zero provider/kind/empty repo stay ErrInvalidArgument on every arm.
- `EnsureAgentForgeSubscription` persists scope+project on the widened UNIQUE.
- New surface: `SubscribersForArtifact` (exact + opened-event container fan-out, one indexed query), `ListForgeNotifyTargets` (LEFT JOIN cursors, container collapse per repo,kind), `UpsertForgeArtifactCursor`, `AdvanceForgeDeliveredRevision` (id+agent-scoped; zero rows -> ErrNotFound).

## Invariant

proto scope enum <-> SQL scope CHECK+UNIQUE <-> Go guard agree on ARTIFACT vs CONTAINER semantics: number is meaningful only under ARTIFACT; CONTAINER rows carry number=0 behind the explicit scope discriminator, with project distinguishing Linear projects.

## Tests

Red-green pgtests: container idempotency, scope-shape rejection (5 arms), Linear project-container distinctness, artifact fan-out (opened on/off, Linear project match/mismatch), targets grouping + container collapse + nil-cursor-before-upsert, advance happy/unknown/foreign-agent. Verified green on real Postgres.

Refs RIG-2732

Co-authored-by: Matt Wilkinson <matt@rigel.build>
@linear-code

linear-code Bot commented Aug 27, 2026

Copy link
Copy Markdown

RIG-2732

@github-actions

github-actions Bot commented Aug 27, 2026

Copy link
Copy Markdown

Compass engineering docs preview: https://compass-forge-rig-2732-t4-ro.compass-eng-docs.pages.dev

Deployed from compass-forge/rig-2732-t4-router at 5ac4f40.

rigel-mintaka and others added 5 commits August 27, 2026 04:16
…r (RIG-2732)

Review fixes for the wire+store slice (PR #674):

- Reconcile the file-header + `ForgeArtifactCursor` struct doc: they claimed this file "never inserts a cursor row" and "the poll driver owns the cursor WRITER", but this slice added `UpsertForgeArtifactCursor` (the shared FETCH-cursor writer) and `ListForgeNotifyTargets` (the notify-target reader). The docs now name those.
- Cover the container-scope cursor-attach branch: `TestListForgeNotifyTargetsContainerCollapse` now upserts a cursor at the container coordinate (number=0), re-lists, and asserts the collapsed container target carries it — exercising the `c.number=0` arm of the LEFT JOIN CASE that was untested.
- Close test gaps: `validSubscriptionCoordinate` unknown-scope default arm; `AdvanceForgeDeliveredRevision` empty-agent / empty-subscription-id guards; a mixed artifact+container `ListForgeNotifyTargets` case pinning the collapse-vs-distinct boundary.

Refs RIG-2732

Co-authored-by: Matt Wilkinson <matt@rigel.build>
T2 of RIG-2732 Piece-2 (webhook-only agent-notification): the event front door for both providers, producing the pipeline's single normalized currency (`forge.ForgeEvent`).

- `internal/forge/notify_event.go`: the `ForgeEvent` normalized-currency struct.
- `internal/forge/githubapp_webhook.go`: `VerifyGitHubSignature` (constant-time HMAC-SHA256 over the raw body) + `ParseGitHubEvent` mapping the Approach event table to normalized events; bodies stripped via `StripOwner` at normalize (the one strip point); `check_suite.completed` carries `HeadSHA` with a nil `ChecksSummary` (router fetches the roll-up).
- `internal/linearagent/data_event.go`: `ParseLinearDataEvent` for Issue/Comment data-change payloads (create->OPENED, update->STATE-iff-updatedFrom-state else UPDATE, comment->COMMENT; remove counted-and-dropped; project id -> `ForgeEvent.Project`).
- `server/github_webhook.go`: `NewGitHubWebhookHandler` — the DL-254 shape (verify -> ack 200 -> enqueue), delivery-id LRU dedup, oversized-body rejection. Unmounted (T7 wires it).

Red-first unit tests cover signature vectors, every event-table row, PR-vs-issue comment discrimination, Linear state-vs-update, project id mapping, `check_suite` HeadSHA+nil-Checks, `StripOwner`, and the handler's signature/oversize/dedup paths.

Refs RIG-2732

Co-authored-by: Matt Wilkinson <matt@rigel.build>
…G-2732)

Review fixes for the webhook ingress slice (PR #675), all reconciling code to the frozen design record (design.md:534-537):

- Body cap 25 MiB -> 1 MiB (design.md:536): keeps the per-request buffered-body allocation small (events are single-digit KB); the old 25 MiB opened a 25x-wider memory-amplification window than the record authorizes.
- Enforce ack-fast: document that `ForgeEventSink.Enqueue` MUST NOT block (it hands off to the async drain loop), and flush the 200 onto the wire before Enqueue so a blocking sink can never delay the client-visible ack.
- Bad/missing signature returns 400, not 401 (design.md:535).
- Add handler tests: verified-but-malformed -> 200 + no enqueue; missing signature header -> 400; secret-unavailable -> 503. Add event-table rows for issues `reopened` -> STATE and pull_request `unlabeled` -> UPDATE.

Refs RIG-2732

Co-authored-by: Matt Wilkinson <matt@rigel.build>
T4: the `ForgeEvent` hot path — `NotifyRouter.Route` + pure `ApplyEvent`/`SnapshotRevision`.

- `notify_router.go`: package-local structural seams (`NotifyStore`, `NotifyDispatcher`, `ChecksRoller`) + `NotifyRouter`. `Route` runs the frozen algorithm (design.md:841-872): load prior snapshot -> resolve CHECKS roll-up before apply -> `ApplyEvent` -> upsert cursor BEFORE notify -> resolve subs (OPENED=container/project-matched, per-artifact=exact coordinate) -> dispatch a `ForgeNotification` carrying revision. Never advances `delivered_revision` (W3, ack-driven).
- `notify_detect.go`: `ArtifactSnapshot` canonical form + pure `ApplyEvent` + `SnapshotRevision` (sha256 of canonical JSON, sorted checks, map-keyed comments) — the cross-producer meeting-point contract shared with T5.
- Red-first unit tests with fakes for both seams, incl. the meeting-point invariant.

Reconciled seams (single-admissible readings, not open forks): `NotifyStore` gained `LoadArtifactCursor` (the frozen 3-method block omits the point read the algorithm's step 1 needs); `ChecksRoller` returns a placeholder `ChecksResult` in this commit, collapsed into the real `forge.ConditionalResult[forge.Checks]` by T5 stacked above; UPDATE/REVIEW are snapshot-neutral and labels are excluded from the digest (the `ForgeEvent` currency carries no title/body/labels, so the canonicalization invariant holds trivially).

Refs RIG-2732

Co-authored-by: Matt Wilkinson <matt@rigel.build>
…-2732)

Review fixes for the notify router slice (PR #676):

- Extend `Route`'s coordinate guard to reject `ev.Number == 0`, not just zero provider/kind. The frozen Global Constraint makes a zero number a caller bug for every event kind (ForgeEvent.Number is always set; on OPENED it is the new artifact's number), so a zero-number event must be `ErrInvalidArgument`, never a bogus number-0 cursor write. OPENED is unaffected (it carries the new artifact's number).
- Add router error-path tests: a `LoadArtifactCursor` error aborts before upsert/dispatch; a `ChecksRoller` error on a CHECKS event aborts before apply; an `UpsertArtifactCursor` error surfaces and dispatches nothing (proving upsert-before-notify ordering under failure). These fakes exposed the error fields but no test set them.

Refs RIG-2732

Co-authored-by: Matt Wilkinson <matt@rigel.build>
@rigel-mintaka
rigel-mintaka force-pushed the compass-forge/rig-2732-t2-ingress branch from e8f7a9f to 4e56465 Compare August 27, 2026 08:51
@rigel-mintaka
rigel-mintaka force-pushed the compass-forge/rig-2732-t4-router branch from 0732b93 to 5ac4f40 Compare August 27, 2026 08:52
@rigel-mintaka
rigel-mintaka marked this pull request as ready for review August 27, 2026 09:05
Base automatically changed from compass-forge/rig-2732-t2-ingress to main August 27, 2026 17:53
@mattwilkinsonn
mattwilkinsonn merged commit c5c6862 into main Aug 27, 2026
34 checks passed
@mattwilkinsonn
mattwilkinsonn deleted the compass-forge/rig-2732-t4-router branch August 27, 2026 17:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants